<template>
  <div class="login-wrap">
    <div class="login-box">
      <!-- 头像 -->
      <div class="avatar-box">
        <img src="../assets/logo.png" alt="" srcset="">
      </div>
      <!-- 表单 -->
      <el-form ref="loginFormRef" :model="loginForm" label-width="0px" :rules="loginFormRules">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-denglu" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-mima" show-password v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="loginFn">登录</el-button>
          <el-button type="info" @click="resetFn">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      // 登录信息校验
      loginFormRules: {
        username: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置
    resetFn () {
      this.$refs.loginFormRef.resetFields()
    },
    // 登录
    loginFn () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return this.$message.error('请按要求填写账号和密码')
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error('登录失败，无效账号或密码')
        window.sessionStorage.setItem('token', res.data.token)
        this.$message.success('登录成功')
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .login-wrap {
    background-color: #274a6c;
    height: 100%;
    .login-box {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%,-50%);
      width: 450px;
      height: 300px;
      background-color: #fff;
      border-radius: 3px;
      .avatar-box {
        position: absolute;
        left: 50%;
        // top: 20%;
        transform: translate(-50%, -60%);
        height: 130px;
        width: 130px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow:  0 0 10px #ddd;
        background-color: #fff;
        img {
          width: 100%;
          height: 100%;
          border-radius: 50%;
          background-color: #eee;
        }
      }
      .el-form {
        position: absolute;
        bottom: 0;
        width: 100%;
        box-sizing: border-box;
        padding: 20px;
        .btns {
          display: flex;
          align-items: center;
          justify-content: space-between;
        }
      }
    }
  }
</style>
